package com.stox.charting.ui.swing.presenter;

import java.awt.Dimension;
import java.awt.Point;
import java.util.List;

import com.stox.charting.ui.swing.view.ChartingInternalFrameView;
import com.stox.core.model.intf.HasBars;
import com.stox.core.ui.swing.widget.MainFrame;
import com.stox.ioc.Container;
import com.stox.navigator.ui.swing.view.NavigatorView;

public class ChartingInternalFrameViewPresenter {

	public void present(List<HasBars> hasBars){
		int count = hasBars.size();
		int rows = 1;
		int columns = 1;
		
		while((rows*columns) < count){
			rows++;
			if((rows*columns) < count){
				columns++;
			}
		}
		
		for(int i = 1; i <= hasBars.size(); i++){
			ChartingInternalFrameView view = new ChartingInternalFrameView();
			MainFrame.getInstance().getDesktop().addView(view);
			setSize(view, hasBars.size(), i, rows, columns);
			setPosition(view, hasBars.size(), i, rows, columns);
			view.afterPropertiesSet();
			view.show();
			view.getPriceChartView().getPriceChart().getModel().setHasBars(hasBars.get(i-1));
			view.repaintChartViews();
		}
	}

	public void setSize(ChartingInternalFrameView chartingInternalFrameView,
			int count, int index, int rows, int columns){
		if(chartingInternalFrameView != null){
			NavigatorView navigatorView = Container.get(NavigatorView.class);
			int width = chartingInternalFrameView.getParent().getSize().width;
			int height = chartingInternalFrameView.getParent().getSize().height;
			int x = navigatorView.getSize().width + navigatorView.getLocation().x;

			if(navigatorView != null && navigatorView.isVisible()
					&& chartingInternalFrameView.getParent().getSize().width > x*2){
				width = chartingInternalFrameView.getParent().getSize().width - x;
				height = chartingInternalFrameView.getParent().getSize().height;
			}
			
			height = height/rows;
			width = width/columns;
			if(index == count){
				width += ((rows*columns)-index)*width;
			}
			chartingInternalFrameView.setSize(new Dimension(width, height));
		}
		
	}
	
	public void setPosition(ChartingInternalFrameView chartingInternalFrameView,
			int count, int index, int rows, int columns){
		if(chartingInternalFrameView != null){NavigatorView navigatorView = Container.get(NavigatorView.class);
			int width = chartingInternalFrameView.getParent().getSize().width;
			int height = chartingInternalFrameView.getParent().getSize().height;
			int x = navigatorView.getSize().width + navigatorView.getLocation().x;
			int y = 0;
			if(navigatorView != null && navigatorView.isVisible()
					&& chartingInternalFrameView.getParent().getSize().width > x*2){
				width = chartingInternalFrameView.getParent().getSize().width - x;
				height = chartingInternalFrameView.getParent().getSize().height;
			}else{
				x = 0;
			}
			
			height = height/rows;
			width = width/columns;
			
			int row = 1;
			int column = 1;
			int counter = 1;
			boolean shouldBreak = false;
			for(int i = 1; i <= rows; i++){
				for(int j = 1; j <= columns; j++){
					if(counter == index){
						row = i;
						column = j;
						shouldBreak = true;
						break;
					}
					counter++;
				}
				if(shouldBreak){
					break;
				}
			}
			
			chartingInternalFrameView.setLocation(new Point(x+(column-1)*width, y+(row-1)*height));
		}
	}
}
